package com.kgc.storeprovider.controller;

import com.kgc.basic.pojo.Good;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.store.pojo.*;
import com.kgc.store.remote.ProCombineRemote;
import com.kgc.storeprovider.service.ProCombineService;
import com.kgc.storeprovider.service.StoreGoodSummaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Controller
@CrossOrigin
public class ProCombineController implements ProCombineRemote {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ProCombineService proCombineService;
    @Autowired
    private StoreGoodSummaryService storeGoodSummaryService;

    @Override
    public ResponseMsg getOne(@RequestParam("code") String code) {
        logger.info("ProCombineController getOne code:{}", code);
        ResponseMsg responseMsg = proCombineService.getByCode(code);
        logger.debug("ProCombineController getOne responseMsg:{}", responseMsg+",code:"+code);
        return responseMsg;
    }

    @Override
    public ResponseMsg getPage(@RequestBody Map<String, Object> map) {
        logger.info("ProCombineController getPage map:{}", map);
        ResponseMsg responseMsg = proCombineService.getByPage(map);
        logger.debug("ProCombineController getPage responseMsg:{}", responseMsg+",map:"+map);
        return responseMsg;
    }

    @Override
    public ResponseMsg add(@RequestBody ProCombine proCombine) {
        logger.info("ProCombineController add proCombine:{}", proCombine);
        ResponseMsg responseMsg = proCombineService.add(proCombine);
        logger.debug("ProCombineController add responseMsg:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg update(@RequestBody ProCombine proCombine) {
        logger.info("ProCombineController update proCombine:{}", proCombine);
        ResponseMsg responseMsg = proCombineService.update(proCombine);
        logger.debug("ProCombineController update responseMsg:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg del(@RequestParam("code") String code) {
        logger.info("ProCombineController del proCombine:{}", code);
        ResponseMsg responseMsg = proCombineService.delete(code);
        logger.debug("ProCombineController del responseMsg:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg changStatus(@RequestParam("code") String code,@RequestParam("code") String status) {
        logger.info("ProCombineController changStatus code:{},status:{}", code, status);
        ResponseMsg responseMsg = proCombineService.changeStatus(code, status);
        logger.debug("ProCombineController changStatus responseMsg:{}", responseMsg+",code:"+code+",status:"+status);
        return responseMsg;
    }

    @Override
    public ResponseMsg changeStore(ProCombine proCombine) {
        logger.info("ProCombineController changeStore proCombine:"+proCombine);
        ResponseMsg responseMsg = new ResponseMsg();
        InStore inStore = proCombineService.createInStoreFromProCombine(proCombine);
        logger.debug("proCombineService createInStoreFromProCombine inStore:{}", inStore);
        OutStore outStore = proCombineService.createOutStoreFromProCombine(proCombine);
        logger.debug("proCombineService createOutStoreFromProCombine responseMsg:"+outStore);
        Map<String,Object> map = new HashMap<>();
        map.put("stock",proCombine.getCount());
        map.put("gid",proCombine.getBom().getProduct().getId());
        map.put("sid",proCombine.getSids().get(0));
        KcStoreGood kcStoreGood = (KcStoreGood)storeGoodSummaryService.selectKcStoreGoodBySidCid((int)map.get("gid"),(int)map.get("sid")).getData();
        BigDecimal stock = new BigDecimal(proCombine.getCount());
        map.put("sumCost",kcStoreGood.getPrice().multiply(stock));
        ResponseMsg msg = storeGoodSummaryService.method(map);
        if (msg.getCode()=="201"){
            map.put("stock",proCombine.getCount()*(-1));
            for(int i=0;i<proCombine.getBom().getMaterial().size();i++) {
                map.put("gid",proCombine.getBom().getMaterial().get(i).getId());
                map.put("sid",proCombine.getSids().get(i+1));
                msg = storeGoodSummaryService.method(map);
                if (msg.getCode()=="201"){
                    break;
                }
            }
        }
        if (msg.getCode()=="200"){
            responseMsg.setCode("200");
        }
        return responseMsg;
    }
}
